const ajax = require('../../tool/ajax');
const screen = require('../component/screen');//截图
let pupConfig, page, config, newUser;

/**
 * 注册页面脚本：不检验图形验证码
 */
const start = async (myPupConfig) => {
  pupConfig = myPupConfig;
  page = pupConfig.page;
  config = pupConfig.config.common;
  newUser = pupConfig.config.newUser;

  await openRegister();
  await fillForm();
}

/**
 * 打开注册页
 */
const openRegister = async () => {
  //通过链接打开注册页
  await page.goto(config.host + 'register');
}

/**
 * 填写表单
 */
const fillForm = async () => {
  //等待页面加载完成
  await page.waitFor('input[type=text]');

  //获取输入框数组
  let inputs1 = await page.$$('input[type=text]');
  let inputs2 = await page.$$('input[type=password]');
  await inputs1[0].type(newUser.id);
  await inputs1[1].type(newUser.tel);
  await inputs1[2].type('5228');
  await inputs1[3].type('123456');
  await inputs2[0].type(newUser.password);
  await inputs2[1].type(newUser.password);

  //点击发送验证码
  let btnCode = await page.$('.send-mobilecode-btn');
  await btnCode.click();
  //等待验证码发送成功
  await ajax.waitForResponse(page, 'getImgValid.do');

  screen(page, '注册');

  //点击提交
  let button = await page.$('button[type=submit]');
  await button.click();
}

module.exports = start;